﻿import flash.geom.Point;
import mx.transitions.easing.Regular;
import mx.transitions.Tween;
import mx.utils.Delegate;
/**
 * ...
 * @author	Kliment
 * @version	1.1.1
 */
class kliment.effect.Flat3D {
	private var _elements:Array;
	private var _space_mc:MovieClip;
	private var _center:Point;
	private var _centerLocal:Point;
	
	public function Flat3D(space_mc:MovieClip, centerX:Number, centerY:Number, elements:Array) {
		_space_mc = space_mc;
		_center = new Point(centerX, centerY);
		_elements = new Array();
		var length:Number = elements.length;
		
		_centerLocal = new Point(_space_mc._x + _space_mc._width * .5, _space_mc._y + _space_mc._height * .5)
		
		for (var i:Number = 0; i < length; i++ )
			addMovieClip(elements[i]);
		_setAction();
	}
	
	public function _setAction():Void {
		_space_mc.onEnterFrame = Delegate.create(this, _move);
	}
	
	private function _move():Void {
		var i:Number = _elements.length;
		var x_mouse:Number = _space_mc._xmouse;
		var y_mouse:Number = _space_mc._ymouse;
		var lx_m:Number = _centerLocal.x - _space_mc._parent._xmouse;
		var ly_m:Number = _centerLocal.y - _space_mc._parent._ymouse;
		while (i--) {
			var item:Object = _elements[i];
			var delta:Number =  item.z / _space_mc._width;
			var x:Number = item.startX  + lx_m * delta + lx_m / item.z;
			item._x += (x - item._x) * .1;
		}
	}
	
	/*private function _move():Void {
		var i:Number = -1;
		var length:Number = _elements.length;
		var x_mouse:Number = _space_mc._xmouse;
		var y_mouse:Number = _space_mc._ymouse;
		var lx_m:Number = _centerLocal.x - _space_mc._parent._xmouse;
		var ly_m:Number = _centerLocal.y - _space_mc._parent._ymouse;
		while (i++ < length) {
			var item:Object = _elements[i];
			var delta:Number =  item.z / _space_mc._width;
			var x:Number = item.startX  + lx_m * delta + lx_m / item.z;
			item._x += (x - item._x) * .1;
		}
	}*/
	
	public function addMovieClip(value:MovieClip, z:Number):Void {
		if (isFinite(z))
			value.z = z;
		value.startX = value._x;
		value.startY = value._y;
		_elements.push(value);
	}
	
	public function remove():Void {
		_space_mc.onEnterFrame = null;
		delete this;
	}
}